package com.tzbank.redis;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang.StringUtils;
import org.springframework.data.redis.core.*;

import com.alibaba.fastjson.JSON;
import com.tzbank.utils.SpringContextUtils;
import com.tzbank.utils.StringConstantUtil;

public class RedisUtils {

	@SuppressWarnings("rawtypes")
	private static RedisTemplate template = SpringContextUtils.getBeanByName("redisTemplate");

	// 向redis里存入数据
	@SuppressWarnings("unchecked")
	public static void setKey(String key, String value) {
		ValueOperations<String, String> ops = template.opsForValue();
		ops.set(key, value);
	}

	// 从redis中取数据
	@SuppressWarnings("unchecked")
	public static String getValue(String key) {
		ValueOperations<String, String> ops = template.opsForValue();
		return ops.get(key);
	}

	// 向redis里存入数据和设置缓存时间（以秒为单位）
	@SuppressWarnings("unchecked")
	public static void setKeyAndCacheTime(String key, String value, long timeout) {
		ValueOperations<String, String> ops = template.opsForValue();
		ops.set(key, value, timeout, TimeUnit.SECONDS);
		
	}

	// 向redis存入的数据做-1 操作
	@SuppressWarnings("unchecked")
	public static void setValueDecrement(String key) {
		template.boundValueOps(key).increment(-1);
	}

	// 向redis存入的数据做+1 操作
	@SuppressWarnings("unchecked")
	public static void setValueIncrement(String key) {
		template.boundValueOps(key).increment(+1);
	}

	// 根据key获取过期时间
	@SuppressWarnings("unchecked")
	public static Long getDeadLine(String key) {
		return template.getExpire(key);
	}

	// 根据key删除缓存
	@SuppressWarnings("unchecked")
	public static void deleteByKey(String key) {
		template.delete(key);
	}

	// 检查key是否存在，返回boolean值
	@SuppressWarnings("unchecked")
	public static Boolean existKey(String key) {
		return template.hasKey(key);
	}

	/**
	 * 获取分页数据
	 * @param key
	 * @param pageNo页码
	 * @param rowsPerPage 每页记录数
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static Set<String> getPageDate(String key,int pageNo,int rowsPerPage) {
		int start = (pageNo-1)*rowsPerPage;
		int end = pageNo*rowsPerPage -1;
		ZSetOperations<String, String> options = template.opsForZSet();
		Set<String> pageDates = options.range(key, start, end);
		return pageDates;
	}
	
	/**
	 * 添加有序集合数据
	 * @param key
	 * @param dataList
	 */
	@SuppressWarnings("unchecked")
	public static void zAddDate(String key,List<Object> dataList) {
		ZSetOperations<String, Object> options = template.opsForZSet();
		int index = 0;
//		Set<TypedTuple<String>> dataSet = new HashSet<TypedTuple<String>>();
		for(Object obj : dataList) {
			options.add(key,obj, index++);
		}
	}
	
	/**
	 * 设置key超时时间
	 * @param key
	 * @param expireTime
	 * @param timeUnit
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static boolean setExpireTime(String key,int expireTime,TimeUnit timeUnit) {
		return template.expire(key,expireTime,timeUnit);

	}
	/**
	 * 查询有序集合总记录数
	 * @param key
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static long getTotal(String key) {
		ZSetOperations<String, String> options = template.opsForZSet();
		Long total = options.count(key, 0, Integer.MAX_VALUE);
		return total;
	}
	
	/**
	 * 获取用户信息
	 * 
	 * @param clientSeqNo
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static Map getUserInfo(String clientSeqNo) {
		ValueOperations<String, String> ops = template.opsForValue();
		String userRedis = ops.get(StringConstantUtil.USER + clientSeqNo);
		if (StringUtils.isEmpty(userRedis)) {
			return null;
		}
		Map map = JSON.parseObject(userRedis, Map.class);
		return map;
	}

	/**
	 * 销毁用户信息
	 * 
	 * @param clientSeqNo
	 */
	public static void destroyUserInfo(String clientSeqNo) {
		deleteByKey(StringConstantUtil.TOKEN + clientSeqNo);
		deleteByKey(StringConstantUtil.USER + clientSeqNo);
	}

	/**
	 * 存入list数据
	 * @param key
	 * @param list
	 */
	public static void setList(String key,List list){
		ListOperations ops = template.opsForList();
		ops.leftPushAll(key,list);
	}

	/**
	 * 存入一个过去过期的值
	 * @param key
	 * @param list
	 * @param time
	 */
	public static void setList(String key,List list,Long time){
		ListOperations ops = template.opsForList();
		template.delete(key);
		ops.leftPushAll(key,list);
		template.expire(key,time,TimeUnit.SECONDS);
	}

	/**
	 * 获取list列表  starIndex,endIndex为0的时候查询所有的
	 * @param key
	 * @param starIndex
	 * @param endIndex
	 * @return
	 */
	public static List getList(String key,Long starIndex,Long endIndex){
		if(starIndex==null){
			starIndex = 0L;
		}
		if(endIndex==null){
			endIndex = -1L;
		}
		ListOperations ops = template.opsForList();
		return ops.range(key,starIndex,endIndex);
	}

	/**
	 * 设置set的集合
	 * @param key
	 * @param set
	 */
	public static void setList(String key,Set set){
		SetOperations ops = template.opsForSet();
		ops.difference(key,set);
	}

	/**
	 * 设置set集合的过期时间
	 * @param key
	 * @param set
	 * @param time
	 */
	public static void setList(String key,Set set,Long time){
		SetOperations ops = template.opsForSet();
		ops.difference(key,set);
		template.expire(key,time,TimeUnit.SECONDS);
	}

	/**
	 * 获取set集合
	 * @param key
	 * @return
	 */
	public static Set getSetList(String key){
		SetOperations ops = template.opsForSet();
		return ops.members(key);
	}
}
